package com.jd.myaaaccount;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.jd.myaaaccount.entity.Family;
import com.jd.myaaaccount.entity.Host;
import com.jd.myaaaccount.model.Common;
import com.jd.myaaaccount.model.FamilyModel;
import com.jd.myaaaccount.util.AlertDialogHelper;
import com.jd.myaaaccount.util.ErrorCode;
import com.jd.myaaaccount.util.OperateCode;

import android.os.Bundle;
import android.os.Handler;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class FamilyActivity extends BaseActivity {

	private ListView lvHome;
	private ImageView ivCreate;
	private ImageView ivJoin;
	private ImageView ivRefresh;
	private Handler handler;

	private FamilyModel fm;

	List<Family> families;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_family);
		fm = new FamilyModel(this);
		iniControl();

		handler = new Handler() {
			@Override
			public void handleMessage(android.os.Message msg) {
				pd.cancel();
				switch (msg.what) {
				case OperateCode.GET_FAMILY:// 获取家庭结果 显示家庭
					if (msg.arg1 == ErrorCode.SUCCESS) {
						List<Map<String, Object>> familyMap = new ArrayList<Map<String, Object>>();
						families = (List<Family>) msg.obj;
						for (Family family : families) {
							Map<String, Object> item = new HashMap<String, Object>();
							item.put("ID", family.getNumber());
							item.put("name", family.getName());
							StringBuilder sb = new StringBuilder();
							for (Host member : family.getMembers()) {
								sb.append(member.getName());
								sb.append(",");
							}
							item.put("members", sb.toString());
							SimpleDateFormat sdf = new SimpleDateFormat(
									"yyyy年MM月dd日");
							item.put("createDate",
									sdf.format(family.getCreateTime()));
							familyMap.add(item);
						}
						SimpleAdapter sa = new SimpleAdapter(
								FamilyActivity.this, familyMap,
								R.layout.layout_family_list_item,
								new String[] { "ID", "name", "members",
										"createDate" },
								new int[] { R.id.tvID, R.id.tvName,
										R.id.tvMember, R.id.tvDate });
						lvHome.setAdapter(sa);
					} else {
						Common.showErrorInfo(FamilyActivity.this, msg.arg1,
								"加载失败", 0);
					}
					break;
				default:
					break;
				}
			};
		};

		// 初始化家庭
		iniFamily();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_home, menu);
		return true;
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == OperateCode.ADD) {
			if (requestCode == ErrorCode.SUCCESS) {
				iniFamily();
			}
		} else if (requestCode == OperateCode.JOIN) {
			if (requestCode == ErrorCode.SUCCESS) {
				iniFamily();
			}
		}

	};

	private void iniControl() {

		ivCreate = (ImageView) findViewById(R.id.ivCreate);
		ivJoin = (ImageView) findViewById(R.id.ivJoin);

		ivCreate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.putExtra("operateFlag", OperateCode.ADD);
				intent.setClass(FamilyActivity.this, EditFamilyActivity.class);
				startActivityForResult(intent, OperateCode.ADD);
			}
		});

		ivJoin.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.putExtra("operateFlag", OperateCode.JOIN);
				intent.setClass(FamilyActivity.this, EditFamilyActivity.class);
				startActivityForResult(intent, OperateCode.JOIN);
			}
		});

		ivRefresh.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				iniFamily();
			}
		});
		lvHome = (ListView) findViewById(R.id.lvHome);
		lvHome.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Family f = families.get(arg2);
				Intent intent = new Intent();
				intent.putExtra("operateFlag", OperateCode.SCAN);
				intent.putExtra("number", f.getNumber());
				intent.putExtra("name", f.getName());
				intent.putExtra("password", f.getPassword());
				intent.setClass(FamilyActivity.this, EditFamilyActivity.class);
				startActivityForResult(intent, OperateCode.SCAN);
			}
		});
	}

	/**
	 * 初始化家庭
	 */
	private void iniFamily() {

		pd = AlertDialogHelper.showWaitProgressDialog(FamilyActivity.this,
				"加载中…");
		new Thread(new Runnable() {
			@Override
			public void run() {
				fm.getMyFamily(handler);
			}
		}).start();
	}
}
